<template>
	<view class="home-receive" v-if="listData.length">
		<view class="row-1">
			<view class="title">领劵中心</view>
			<view class="tips">
				<text>先领卷</text>
				<text style="margin-left: 16rpx;">更优惠</text>
			</view>
		</view>

		<view class="row-2" v-for="(item,index) in listData" :key="index">
			<view v-if="item.length == 2">
				<view class="box">
					<view class="box-item" v-for="(i,s) in item" :key="s" :class="s == 1? 'blue' : ''">
						<view class="price">
							<text class="price-icon">￥</text>
							<text class="price-numb">{{i.couponValue}}</text>
						</view>
						<view class="btn" @click="handleReceive(i)">
							<view class="">立即</view>
							<view class="">领取</view>
						</view>
						<view class="tips">{{Number(i.couponType)==1?('满减'+i.minMonetary+'元使用'):'直减'}}</view>
					</view>
				</view>
			</view>
			<view v-else>
				<view class="big-box" v-for="(i,s) in item" :key="s">
					<view class="price">
						<text class="price-icon">￥</text>
						<text class="price-numb">{{i.couponValue}}</text>
					</view>
					<view class="">
						<view class="tips">满减99元使用</view>
						<view class="time">使用时间：{{i | filTime}}</view>
					</view>
					<view class="btn" @click="handleReceive(i)">
						<view class="">立即</view>
						<view class="">领取</view>
					</view>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		listReceive,
		listSaveReceive
	} from "@/culturalCreativity/http/home.js";
	import {} from "@/utils/pay.js";
	import {
		dateFormat
	} from "@/utils/pay.js";
	export default {
		data() {
			return {
				listData: [],
				openid: uni.getStorageSync('openid'),
			}
		},
		methods: {
			async init() {
				let res = await listReceive();
				let startArr = [];
				let endArr = [];
				res.data.rows.forEach((item, index) => {
					if (startArr.length == 2) {
						endArr.push(startArr);
						startArr = [];
						startArr.push(item)
					} else {
						startArr.push(item);
					};
					if ((res.data.rows.length - 1) == index) {
						endArr.push(startArr);
					}
				});
				this.listData = endArr;
			},
			async handleReceive(options) {
				try {
					uni.showLoading({
						title: '领取中',
						mask: true
					});
					let data = {
						...options,
						userId: this.openid,
						couponId: options.couponId,
						couponNumber: options.distributionTimes || 1,
						limitTime: options.limitTime,
						phone: uni.getStorageSync('phone'),
						isUse: 0
					};
					let date = new Date();
					let validiteStart = '';
					let	validiteEnd = '';
					date.setDate(date.getDate() + 3);
					if (options.validiteEnd && options.validiteStart) {
						data.validiteStart = options.validiteStart;
						data.validiteEnd = options.validiteEnd;
						
						console.log(options.validiteEnd,options.validiteStart);
					} else {
						data.validiteStart = dateFormat('YYYY-mm-dd',new Date());
						data.validiteEnd = dateFormat('YYYY-mm-dd',date);
					}
					let keys = {
						userId: null,
						phone: null,
						couponId: null,
						couponName: null,
						couponType: null,
						minMonetary: null,
						couponValue: null,
						validiteEnd: null,
						validiteStart: null,
						isUse: null,
						goodsId: null,
						goodsName: null,
						issuperposition: null,
						useGoods: null,
						couponNumber: null,
						limitTime: null
					};
					Object.keys(keys).forEach(key => {
						keys[key] = data[key] || 0;
					});
					let res = await listSaveReceive(keys);
					uni.showToast({
						icon: "success",
						title: "领取成功",
						duration: 2000,
						mask: true
					});
					this.init();
				} catch (e) {
					uni.hideLoading();
					console.error(`home-receive.vue`, e)
				}
			}
		},
		filters: {
			filTime(options) {
				if (!(options.getCouponStartTime && options.getCouponEndTime)) return '';
				let str1 = dateFormat('YYYY年m.d', new Date(options.getCouponStartTime.replaceAll('-', '/')));
				let str2 = dateFormat('m.d', new Date(options.getCouponEndTime.replaceAll('-', '/')));
				return `${str1}-${str2}`;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.home-receive {
		background-color: #FFFFFF;
		border-radius: 30rpx;
		padding: 24rpx;
		box-sizing: border-box;

		.row-2 {
			.big-box {
				display: flex;
				align-items: center;
				position: relative;
				width: 100%;
				height: 130rpx;
				margin-top: 24rpx;
				background: url() center/cover no-repeat;

				.price {
					color: #FFFFFF;
					margin: 0 24rpx 0 47rpx;
					display: flex;
					align-items: flex-end;

					.price-icon {
						font-size: 28rpx;
						margin-bottom: 22rpx;
					}

					.price-numb {
						font-size: 90rpx;
						font-weight: 600;
					}
				}

				.time {
					color: rgba(255, 232, 201, 1);
					font-weight: 500;
					line-height: 30rpx;
					font-size: 20rpx;
					margin-top: 14rpx;
				}

				.btn {
					position: absolute;
					top: 32rpx;
					right: 24rpx;
					line-height: 33rpx;
					font-size: 24rpx;
					font-family: PingFangSC-Medium, PingFang SC;
					font-weight: 500;
					color: #FFFFFF;
				}

				.tips {
					background: #FFFFFF;
					border-radius: 18rpx;
					text-align: center;
					font-size: 20rpx;
					font-weight: 500;
					color: #FEB12D;
					line-height: 35rpx;
					width: 160rpx;
					height: 35rpx;
				}
			}

			.box {
				display: flex;
				justify-content: space-between;
				margin-top: 24rpx;

				.box-item {
					position: relative;
					width: 307rpx;
					height: 130rpx;
					background: url() center/cover no-repeat;

					.price {
						position: absolute;
						top: 12rpx;
						left: 24rpx;
						width: 150rpx;
						display: flex;
						align-items: flex-end;
						justify-content: center;
						color: #FFFFFF;

						.price-icon {
							font-size: 24rpx;
							margin-bottom: 10rpx;
						}

						.price-numb {
							font-size: 50rpx;
							font-weight: 600;
						}
					}

					.btn {
						position: absolute;
						top: 32rpx;
						right: 24rpx;
						line-height: 33rpx;
						font-size: 24rpx;
						font-family: PingFangSC-Medium, PingFang SC;
						font-weight: 500;
						color: #FFFFFF;
					}

					.tips {
						position: absolute;
						left: 24rpx;
						bottom: 13rpx;
						width: 160rpx;
						height: 35rpx;
						background: #FFFFFF;
						border-radius: 18rpx;
						text-align: center;
						font-size: 20rpx;
						font-weight: 500;
						color: #FEB12D;
						line-height: 35rpx;
					}
				}

				.box-item.blue {
					background: url() center/cover no-repeat;

					.tips {
						color: rgba(83, 168, 254, 1);
					}
				}
			}
		}

		.row-1 {
			display: flex;
			justify-content: space-between;
			align-items: flex-end;

			.title {
				color: rgba(253, 143, 25, 1);
				font-size: 32rpx;
			}

			.tips {
				text {
					color: rgba(253, 143, 25, 1);
					font-size: 24rpx;
				}
			}
		}
	}
</style>
